/**
 * Created by red on 2017/4/30.
 */
import { parse } from 'utils/';
/**
 * 问题管理
 * @type {Object}
 */
export default [
  {
    name: '问题列表',
    method: 'list',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query(
        'question_banks',
        query => {
          if (!!data.params.chapter_title) query.contains('chapter_title', data.params.chapter_title);
          query.count().then(res => data.page.total = res, err => data.page.total = 0);
          query.skip((data.page.current_page - 1) * data.page.page_size);
          query.limit(data.page.page_size);
        }
      ).filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            question_index: item.get('question_index'),
            chapter_title: item.get('chapter_title'),
            question: item.get('question'),
            gold_coin: item.get('gold_coin'),
            silver_coin: item.get('silver_coin'),
            paragraph: item.get('paragraph'),
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '查询问题',
    method: 'find',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('question_banks', query => query.equalTo('objectId', data))
        .filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            question_index: item.get('question_index'),
            chapter_title: item.get('chapter_title'),
            chapter_id: item.get('chapter_id'),
            question: item.get('question'),
            gold_coin: item.get('gold_coin'),
            paragraph: item.get('paragraph'),
            answer_list: item.get('answer_list')
          }));
        })
        .subscribe(next => fn(next[0]));
    }
  },
  {
    name: '新增问题',
    method: 'create',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.create('question_banks', data).subscribe(fn, errFn);
    }
  },
  {
    name: '修改问题',
    method: 'update',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      console.log('data:::', data);
      parse.query('question_banks', query => query.equalTo('objectId', data.id))
        .filter(x => x.type && x.type === 'result')
        .map(x => x.result[0])
        .subscribe(res => {
          parse.update(res, data).subscribe(fn, errFn);
        });
    }
  },
  {
    name: '删除问题',
    method: 'delete',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('question_banks', query => query.equalTo('objectId', data))
        .filter(x => x.type && x.type === 'result')
        .map(x => x.result[0])
        .subscribe(res => {
          parse.delete(res).subscribe(fn, errFn);
        });
    }
  }
];